package com.xaslsoft.xaslsoft.device.printer.jiabo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PrintUtil {
	private static PrintJB pjb = PrintJB.INSTANCE;
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyMMddHHmmss");

	public static void printLabel(List<InvertoryInfo> list) {
		System.setProperty("jna.encoding", "GBK");
		pjb.openport("Gprinter  GP-2120TL");
		pjb.setup("100", "200", "5", "8", "0", "0", "0");
		pjb.clearbuffer();
		pjb.windowsfont(120, 0, 48, 0, 0, 0, "宋体", "领料清单");
		int height = 100;
		pjb.windowsfont(5, height, 26, 0, 0, 0, "宋体", "单号:" + sdf.format(new Date()));
		height += 70;
		pjb.windowsfont(5, height, 32, 0, 0, 0, "宋体", "PO号");
		pjb.windowsfont(160, height, 32, 0, 0, 0, "宋体", "物料描述");
		pjb.windowsfont(330, height, 32, 0, 0, 0, "宋体", "数量");
		for (InvertoryInfo invertoryInfo : list) {
			String desc = invertoryInfo.getPdesc();
			int line = (desc.length() / 8) + 1;
			height = height + 45;
			pjb.windowsfont(5, height, 26, 0, 0, 0, "宋体", invertoryInfo.getPcode());
			pjb.windowsfont(380, height, 26, 0, 0, 0, "宋体", invertoryInfo.getActNumber() + "");
			if (line > 1) {
				for (int j = 0; j < line; j++) {
					String substring = desc.substring(8 * j, 8 * (j + 1) > desc.length() ? desc.length() : 8 * (j + 1));
					pjb.windowsfont(130, height, 26, 0, 0, 0, "宋体", substring);
					height = height + 45;
					if (height > 800) {
						pjb.printlabel("1", "1");
						pjb.clearbuffer();
						height = 0;
					}
				}
			}
		}
		pjb.windowsfont(200, height + 100, 28, 0, 0, 0, "宋体", "签字:");
		pjb.printlabel("1", "1");
		pjb.closeport();
	}

	public static void main(String[] args) {
		ArrayList<InvertoryInfo> arrayList = new ArrayList<>();
		for (int i = 0; i < 4; i++) {
			InvertoryInfo invertoryInfo = new InvertoryInfo();
			invertoryInfo.setPcode("4955910525");
			invertoryInfo.setPdesc("盐酸标准滴定溶液 ,1mol/L（沪试）,500ml,国药");
			invertoryInfo.setActNumber(5);
			arrayList.add(invertoryInfo);
		}
		printLabel(arrayList);
	}
}


